home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH5 / EMAGA5 BOOK CODE / control / server / misc / item.cs
Text File  |  2006-06-23  |  7KB  |  235 lines

  1. //============================================================================
  2. // control/misc/items.cs
  3. //
  4. //
  5. //  Copyright (c) 2003,2006  by Kenneth C.  Finney.
  6. //============================================================================
  7.  
  8. // These scripts make use of dynamic attribute values on Item datablocks,
  9. // these are as follows:
  10. //
  11. //    maxInventory      Max inventory per object (100 bullets per box, etc.)
  12. //    pickupName        Name to display when client pickups item
  13. //
  14. // Item objects can have:
  15. //
  16. //    count             The # of inventory items in the object.  This
  17. //                      defaults to maxInventory if not set.
  18.  
  19. // Respawntime is the amount of time it takes for a static "auto-respawn"
  20. // object, such as an ammo box or weapon, to re-appear after it's been
  21. // picked up.  Any item marked as "static" is automaticlly respawned.
  22. $Item::RespawnTime = 20 * 1000;
  23.  
  24. // Poptime represents how long dynamic items (those that are thrown or
  25. // dropped) will last in the world before being deleted.
  26. $Item::PopTime = 10 * 1000;
  27.  
  28.  
  29. //-----------------------------------------------------------------------------
  30. // ItemData base class methods used by all items
  31. //-----------------------------------------------------------------------------
  32.  
  33. //-----------------------------------------------------------------------------
  34.  
  35. function Item::respawn(%this)
  36. {
  37.    // This method is used to respawn static ammo and weapon items
  38.    // and is usually called when the item is picked up.
  39.    // Instant fade...
  40.    %this.startFade(0, 0, true);
  41.    %this.setHidden(true);
  42.  
  43.    // Shedule a reapearance
  44.    %this.schedule($Item::RespawnTime, "hide", false);
  45.    %this.schedule($Item::RespawnTime + 100, "startFade", 1000, 0, false);
  46. }
  47.  
  48. function Item::schedulePop(%this)
  49. {
  50.    // This method deletes the object after a default duration. Dynamic
  51.    // items such as thrown or drop weapons are usually popped to avoid
  52.    // world clutter.
  53.    %this.schedule($Item::PopTime - 1000, "startFade", 1000, 0, true);
  54.    %this.schedule($Item::PopTime, "delete");
  55. }
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // Callbacks to hook items into the inventory system
  60.  
  61. function ItemData::onThrow(%this,%user,%amount)
  62. {
  63.    // Remove the object from the inventory
  64.    if (%amount $= "")
  65.       %amount = 1;
  66.    if (%this.maxInventory !$= "")
  67.       if (%amount > %this.maxInventory)
  68.          %amount = %this.maxInventory;
  69.    if (!%amount)
  70.       return 0;
  71.    %user.decInventory(%this,%amount);
  72.  
  73.    // Construct the actual object in the world, and add it to
  74.    // the mission group so it's cleaned up when the mission is
  75.    // done.  The object is given a random z rotation.
  76.    %obj = new Item() {
  77.       datablock = %this;
  78.       rotation = "0 0 1 " @ (getRandom() * 360);
  79.       count = %amount;
  80.    };
  81.    MissionGroup.add(%obj);
  82.    %obj.schedulePop();
  83.    return %obj;
  84. }
  85.  
  86. function ItemData::onPickup(%this,%obj,%user,%amount)
  87. {
  88.    // Add it to the inventory, this currently ignores the request
  89.    // amount, you get what you get.  If the object doesn't have
  90.    // a count or the datablock doesn't have maxIventory set, the
  91.    // object cannot be picked up.
  92.    %count = %obj.count;
  93.    if (%count $= "")
  94.       if (%this.maxInventory !$= "") {
  95.          if (!(%count = %this.maxInventory))
  96.             return;
  97.       }
  98.       else
  99.          %count = 1;
  100.    %user.incInventory(%this,%count);
  101.  
  102.    // Inform the client what they got.
  103.    if (%user.client)
  104.       messageClient(%user.client, 'MsgItemPickup', '\c0You picked up %1', %this.pickupName);
  105.  
  106.    // If the item is a static respawn item, then go ahead and
  107.    // respawn it, otherwise remove it from the world.
  108.    // Anything not taken up by inventory is lost.
  109.    if (%obj.isStatic())
  110.       %obj.respawn();
  111.    else
  112.       %obj.delete();
  113.    return true;
  114. }
  115.  
  116. //-----------------------------------------------------------------------------
  117. // Hook into the mission editor.
  118.  
  119. function ItemData::create(%data)
  120. {
  121.    // The mission editor invokes this method when it wants to create
  122.    // an object of the given datablock type.  For the mission editor
  123.    // we always create "static" re-spawnable rotating objects.
  124.    %obj = new Item() {
  125.       dataBlock = %data;
  126.       static = true;
  127.       rotate = true;
  128.    };
  129.    return %obj;
  130. }
  131.  
  132. datablock ItemData(Copper)
  133. {
  134.    // Mission editor category, this datablock will show up in the
  135.    // specified category under the "shapes" root category.
  136.    category = "Coins";
  137.  
  138.    // Basic Item properties
  139.    shapeFile = "~/data/models/items/kash1.dts";
  140.    mass = 0.7;
  141.    friction = 0.8;
  142.    elasticity = 0.3;
  143.  
  144.  
  145.    respawnTime = 30 * 60000;
  146.    salvageTime = 15 * 60000;
  147.    // Dynamic properties defined by the scripts
  148.    pickupName = "a copper coin";
  149.    value = 1;
  150. };
  151.  
  152. datablock ItemData(Silver)
  153. {
  154.    // Mission editor category, this datablock will show up in the
  155.    // specified category under the "shapes" root category.
  156.    category = "Coins";
  157.  
  158.    // Basic Item properties
  159.    shapeFile = "~/data/models/items/kash100.dts";
  160.    mass = 0.7;
  161.    friction = 0.8;
  162.    elasticity = 0.3;
  163.  
  164.  
  165.    respawnTime = 30 * 60000;
  166.    salvageTime = 15 * 60000;
  167.    // Dynamic properties defined by the scripts
  168.    pickupName = "a silver coin";
  169.    value = 100;
  170. };
  171.  
  172. datablock ItemData(Gold)
  173. {
  174.    // Mission editor category, this datablock will show up in the
  175.    // specified category under the "shapes" root category.
  176.    category = "Coins";
  177.  
  178.    // Basic Item properties
  179.    shapeFile = "~/data/models/items/kash1000.dts";
  180.    mass = 0.7;
  181.    friction = 0.8;
  182.    elasticity = 0.3;
  183.  
  184.  
  185.    respawnTime = 30 * 60000;
  186.    salvageTime = 15 * 60000;
  187.    // Dynamic properties defined by the scripts
  188.    pickupName = "a gold coin";
  189.    value = 1000;
  190. };
  191.  
  192.  
  193. //-----------------------------------------------------------------------------
  194. // Health Patchs cannot be picked up and are not meant to be added to
  195. // inventory.  Health is applied automatically when an objects collides
  196. // with a patch.
  197. //-----------------------------------------------------------------------------
  198.  
  199. datablock ItemData(FirstAidKit)
  200. {
  201.    // Mission editor category, this datablock will show up in the
  202.    // specified category under the "shapes" root category.
  203.    category = "Health";
  204.  
  205.    // Basic Item properties
  206.    shapeFile = "~/data/models/items/healthPatch.dts";
  207.    mass = 1;
  208.    friction = 1;
  209.    elasticity = 0.3;
  210.  
  211.    respawnTime = 600000;
  212.    // Dynamic properties defined by the scripts
  213.    repairAmount = 200;
  214.    maxInventory = 0; // No pickup or throw
  215. };
  216.  
  217. function FirstAidKit::onCollision(%this,%obj,%col)
  218. {
  219.    // Apply health to colliding object if it needs it.
  220.    // Works for all shapebase objects.
  221.    if (%col.getDamageLevel() != 0 && %col.getState() !$= "Dead" )
  222.    {
  223.    echo("!!@@@@@@@@@@@@@ col"@%col);
  224.       %col.applyRepair(%this.repairAmount);
  225.       %obj.respawn();
  226.       if (%col.client)
  227.       {
  228.          messageClient
  229.               (%col.client,'MSG_Treatment','\c2Medical treatment applied');
  230.       }
  231.    }
  232. }
  233.  
  234.  
  235.